/*
 * Copyright (c) 2011 Ball State University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package edu.bsu.android.gtv.apps.bconnected.data;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.util.Log;

import edu.bsu.android.gtv.apps.bconnected.objects.Course;
import edu.bsu.android.gtv.apps.bconnected.parser.ParserFactory;
import edu.bsu.android.gtv.apps.bconnected.parser.ServiceParser;
import edu.bsu.android.gtv.apps.bconnected.util.Constants;

/**
 * Data layer for the {@link Course} object
 * @author Kyle Parker, University Computing Services
 */
public class CourseData {
	private static ArrayList<Course> mCourses;
    private static ArrayList<Course> mOtherCourses;
    private static ArrayList<Course> mSharedCourses;

    /**
     * Loads an ArrayList<Course> from the service handler containing the list of courses based on the student's BSU ID
     * and the selected term ID.
     * This is the "schedule" list for the student during a given term
     * @param bsuID
     * @param termID
     * @param loginGuid - login guid used to determine whether the user is authorized to retrieve the data from the service handler
     * @param activity - used by the {@link ParserFactory} to retrieve the demo_account shared preference - this will determine what
     * 					 data source to use for the service handler.
     * @throws UnsupportedEncodingException - the encrypted BSU ID has to be encoded in order to pass as a querystring parameter
     */
    public static void initializeCourse(String bsuID, String termID, String loginGuid, Activity activity) 
    		throws UnsupportedEncodingException { 
    	List<String> querystringParms = new ArrayList<String>(3);
		querystringParms.add(Constants.QUERYSTRING_TERM_ID + termID); 
		querystringParms.add(Constants.QUERYSTRING_BSU_ID + URLEncoder.encode(bsuID, "UTF-8")); 
		querystringParms.add(Constants.QUERYSTRING_LOGIN_GUID + loginGuid);
			
		ServiceParser parser = ParserFactory.getParser(Constants.SERVICE_TYPE_SCHEDULE, querystringParms, activity);
		mCourses = parser.parseCourse();
		
		// Sort the courses
		Collections.sort(mCourses, Collections.reverseOrder());
    }

    /**
     * Loads an ArrayList<Course> from the service handler containing the list of courses based on the instructor's BSU ID
     * This is the list of all courses taught by an instructor for the current and all future terms
     * @param bsuID
     * @param activity - used by the {@link ParserFactory} to retrieve the demo_account shared preference - this will determine what
     * 					 data source to use for the service handler.
     * @throws UnsupportedEncodingException - the encrypted BSU ID has to be encoded in order to pass as a querystring parameter
     */
    public static void initializeOtherCourses(String bsuID, Activity activity) 
    		throws UnsupportedEncodingException {
    	List<String> querystringParms = new ArrayList<String>(4);
		querystringParms.add(Constants.QUERYSTRING_BSU_ID + URLEncoder.encode(bsuID, "UTF-8")); 

		ServiceParser parser = ParserFactory.getParser(Constants.SERVICE_TYPE_OTHER_COURSES, querystringParms, activity);
		mOtherCourses = parser.parseCourse();
		
		// Sort the courses
		Collections.sort(mOtherCourses, Collections.reverseOrder());
    }

    /**
     * Loads an ArrayList<Course> from the service handler containing the list of courses based on the both the student's and 
     * classmate's BSU ID, term number and year
     * This is the list of all courses that a student and classmate share for a given term
     * @param bsuID
     * @param classmateBSUID 
     * @param termNumber
     * @param termYear
     * @param loginGuid - login guid used to determine whether the user is authorized to retrieve the data from the service handler
     * @param activity - used by the {@link ParserFactory} to retrieve the demo_account shared preference - this will determine what
     * 					 data source to use for the service handler.
     * @throws UnsupportedEncodingException - the encrypted BSU ID has to be encoded in order to pass as a querystring parameter
     */
    public static void initializeSharedCourses(String bsuID, String classmateBSUID, String termNumber, String termYear, 
    		String loginGuid, Activity activity) throws UnsupportedEncodingException {
    	List<String> querystringParms = new ArrayList<String>(5);
    	
    	querystringParms.add(Constants.QUERYSTRING_BSU_ID + URLEncoder.encode(bsuID, "UTF-8"));
    	querystringParms.add(Constants.QUERYSTRING_CLASSMATE_BSU_ID + URLEncoder.encode(classmateBSUID, "UTF-8"));
		querystringParms.add(Constants.QUERYSTRING_TERM_NUM + termNumber); 
		querystringParms.add(Constants.QUERYSTRING_TERM_YEAR + termYear); 
		querystringParms.add(Constants.QUERYSTRING_LOGIN_GUID + loginGuid);
		
		ServiceParser parser = ParserFactory.getParser(Constants.SERVICE_TYPE_SHARED_COURSE, querystringParms, activity);
		mSharedCourses = parser.parseCourse();
		
		// Sort the courses
		Collections.sort(mSharedCourses, Collections.reverseOrder());
    }

    /**
     * Loads an ArrayList<Course> from the service handler containing the list of courses based on the both the student's and 
     * classmate's BSU ID
     * This is the list of all courses that a student and classmate share for the current and all future terms
     * @param bsuID
     * @param classmateBSUID 
     * @param loginGuid - login guid used to determine whether the user is authorized to retrieve the data from the service handler
     * @param activity - used by the {@link ParserFactory} to retrieve the demo_account shared preference - this will determine what
     * 					 data source to use for the service handler.
     * @throws UnsupportedEncodingException - the encrypted BSU ID has to be encoded in order to pass as a querystring parameter
     */
    public static void initializeSharedCourses(String bsuID, String classmateBSUID, String loginGuid, Activity activity) 
    		throws UnsupportedEncodingException {
    	List<String> querystringParms = new ArrayList<String>(3);
    	
    	querystringParms.add(Constants.QUERYSTRING_BSU_ID + URLEncoder.encode(bsuID, "UTF-8"));
    	querystringParms.add(Constants.QUERYSTRING_CLASSMATE_BSU_ID + URLEncoder.encode(classmateBSUID, "UTF-8"));
		querystringParms.add(Constants.QUERYSTRING_LOGIN_GUID + loginGuid);
		
		ServiceParser parser = ParserFactory.getParser(Constants.SERVICE_TYPE_SHARED_COURSE, querystringParms, activity);
		mSharedCourses = parser.parseCourse();
		
		// Sort the courses
		Collections.sort(mSharedCourses, Collections.reverseOrder());
    }

    /**
     * Returns the integer position of a {@link Course} object within the ArrayList<Course> based on a specific course reference number (CRN)
     * @param crn - course reference number used to uniquely identify a course
     * @return i|0
     */
    public static int getCourseByCRN(String crn) {
		for (int i = 0; i < mCourses.size(); i++) {
			Log.i("CourseData", mCourses.get(i).getCourseRefNum().trim());
	    	if (mCourses.get(i).getCourseRefNum().equals(crn)) {
	    		return i;
	    	}
        }
        
        return 0;
    }

    /**
     * Returns a boolean indicating whether or not the student was authenticated to retrieve the course data from the service handler. 
     * If the BSU ID and login guid passed to the service handler do not match the values in the SQL Server backend database, then the XML 
     * will contain an error indicator in the first element. 
     * @return true|false
     */
    public static boolean getCourseAuth() {
    	if (mCourses.size() > 0) {
    		if (mCourses.get(0).getError() != null) {
    			return false;
    		}
    	}
    	
    	return true;
    }

    /**
     * Returns the integer value of the {@link Course} object size
     * @return mCourses.size() | 0
     */
    public static int getCourseCount() {
    	if (mCourses == null) {
    		return 0;
    	} else {
            return mCourses.size();
    	}
    }

    /**
     * Returns a {@link Course} object at the index specified
     * @param i - the index of the object to be returned
     * @return mCourses.get(i)
     */
    public static Course getCourse(int i) {
        return mCourses.get(i);
    }

    /**
     * Returns the ArrayList<Course>
     * @return mCourses
     */
	public static ArrayList<Course> getCourses() {		
		return mCourses;
	}

    /**
     * Returns the integer value of the {@link Course} object size
     * @return mOtherCourses.size() | 0
     */
    public static int getOtherCoursesCount() {
    	if (mOtherCourses == null) {
    		return 0;
    	} else {
            return mOtherCourses.size();
    	}
    }

    /**
     * Returns a {@link Course} object at the index specified
     * @param i - the index of the object to be returned
     * @return mOtherCourses.get(i)
     */
    public static Course getOtherCourses(int i) {
        return mOtherCourses.get(i);
    }

    /**
     * Returns the ArrayList<Course>
     * @return mOtherCourses
     */
    public static ArrayList<Course> getOtherCourses() {
        return mOtherCourses;
    }

    /**
     * Returns a boolean indicating whether or not the student was authenticated to retrieve the shared course data from the service handler. 
     * If the BSU ID and login guid passed to the service handler do not match the values in the SQL Server backend database, then the  
     * XML will contain an error indicator in the first element. 
     * @return true|false
     */
    public static boolean getSharedCourseAuth() {
    	if (mSharedCourses.size() > 0) {
    		if (mSharedCourses.get(0).getError() != null) {
    			return false;
    		}
    	}
    	
    	return true;
    }

    /**
     * Returns the integer value of the {@link Course} object size
     * @return mContent() | 0
     */
    public static int getSharedCoursesCount() {
        return mSharedCourses.size();
    }
 
    /**
     * Returns a {@link Course} object at the index specified
     * @param i - the index of the object to be returned
     * @return mSharedCourses.get(i);
     */
    public static Course getSharedCourses(int i) {
        return mSharedCourses.get(i);
    }

    /**
     * Returns the ArrayList<Course>
     * @return mSharedCourses
     */
    public static ArrayList<Course> getSharedCourses() {
        return mSharedCourses;
    }
}
